
*-------------------------*
*-------------------------*
*     Main Tables
*------------------------*
*-------------------------*

*---------------------------------*
*LOAD DATA SET USED FOR TABLE 1
*---------------------------------*

use "created_dta/${wholesamp}", clear

*************
*  Table 1
*************	
	cap gen hsg = init_civedcatg == "HSG"
	cap gen smcplus = inlist(init_civedcatg,"ASC","CLG","GRD","SMC")
	qui  ivreghdfe noncom_death_by_8 (${NEWENDOG}_as = ${NEWINST}_as) if ${rest} >= 8 & an_samp_dum == 1 , absorb(mos_tc_post_yr_terms_an_samp) robust $ivregsettings
	g temp_as = 1 if e(sample)
	qui {
		
	foreach var in init_approx_age married black hisp other_race female init_nrdep hsd_ged hsg smcplus init_afqsc combat_occ mths_dep_win_3yr wia_by_12 combat_death_by_12  { 
		foreach grp in full as a0515 depas ndepas {
			local cond
			if "`grp'" == "as" local cond "if temp_as == 1"
			if "`grp'" == "nodep" local cond "if ever_dep_q32 == 0"			
			if "`grp'" == "dep" local cond "if ever_dep_q32 == 1"
			if "`grp'" == "ndepas" local cond "if ever_dep_q32 == 0 & temp_as == 1"			
			if "`grp'" == "depas" local cond "if ever_dep_q32 == 1 & temp_as == 1"
			if "`grp'" == "a0515" local cond `"if inrange(year_arrival,2005,2015)==1 & init_sex == "M""'
			summ `var' `cond'
			local `grp'_`var' = r(mean)
			if inlist("`var'","married","black","hisp","other_race","female","hsd_ged") local `grp'_`var' = ``grp'_`var'' * 100
			if inlist("`var'","hsg","smcplus","combat_occ" ) local `grp'_`var' = ``grp'_`var'' * 100
		}
	}

*Restrict cols 3 & 4 to Analysis Sample
		cap file close b
	file open b using "${outputfolder}/Table1_panela.tex", write replace
	foreach var in init_approx_age married black hisp other_race female init_nrdep hsd_ged hsg smcplus init_afqsc {
		local dec 2
		if inlist("`var'","init_approx_age","init_nrdep","init_afqsc") local dec 2
		if "`var'" == "init_approx_age" local dvlab "Age"
		if "`var'" == "married" local dvlab "Married"
		if "`var'" == "black" local dvlab "Black"
		if "`var'" == "hisp" local dvlab "Hispanic"
		if "`var'" == "other_race" local dvlab "Other Race"
		if "`var'" == "female" local dvlab "Female"
		if "`var'" == "init_nrdep" local dvlab "Number of Dependents"
		if "`var'" == "hsd_ged" local dvlab "HS Dropout or GED"
		if "`var'" == "hsg" local dvlab "HS Graduate"
		if "`var'" == "smcplus" local dvlab "Some College+"
		if "`var'" == "init_afqsc" local dvlab "AFQT Score"

		file write b "`dvlab'" _char(38) %5.`dec'f (`full_`var'') _char(38) %5.`dec'f (`a0515_`var'') _char(38) %5.`dec'f (`as_`var'') _char(38) %5.`dec'f (`ndepas_`var'') _char(38) %5.`dec'f (`depas_`var'') _char(92) _char(92) _n
	}
	file close b
	
	file open b using "${outputfolder}/Table1_panelb.tex", write replace
	foreach var in combat_occ mths_dep_win_3yr wia_by_12 combat_death_by_12 {
		local dec 2
		if inlist("`var'","mths_dep_win_3yr") local dec 2
		if "`var'" == "combat_occ" local dvlab "Combat Occupation"
		if "`var'" == "mths_dep_win_3yr" local dvlab "Mths Deployed w/in 3 yrs"
		if "`var'" == "wia_by_12" local dvlab "Combat Injury w/in 3 yrs"
		if "`var'" == "combat_death_by_12" local dvlab "Combat Death w/in 3 yrs"


		file write b "`dvlab'" _char(38) %5.`dec'f (`full_`var'') _char(38) %5.`dec'f (`a0515_`var'') _char(38) %5.`dec'f (`as_`var'') _char(38) %5.`dec'f (`ndepas_`var'') _char(38) %5.`dec'f (`depas_`var'')  _char(92) _char(92) _n
		
		if "`var'" == "combat_death_by_12" {
			file write b "\hline" _n
			file write b "Observations" _char(38) "782,232" _char(38) "483,367" _char(38) "157,415" _char(38) "53,425" _char(38) "103,990" _n
		}
	}
	file close b
}


*LOAD DATA SET USED FOR TABLES 2-5
use "created_dta/${analysissamp}", clear

*************
*  Table 2   
*************

foreach SPEC in FE1 {

foreach INST in NEWINST NEWENDOG  { 

eststo clear

foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { 
areg `cov' , a(${`SPEC'})
predict r`cov', resid
local dof = e(N) - e(k_absorb)
areg ${`INST'} , a(${`SPEC'})
cap drop indinst
predict indinst, resid
if "`cov'"!= "init_afqsc" reg r`cov' indinst, dof(`dof')
estimates store n`cov'
if "`cov'"== "init_afqsc"  reg r`cov'  indinst afqs_mi, dof(`dof')
estimates store n`cov'
}

suest nblack nhisp nother_race nmarried ninit_nrdep nhsgplus ninit_approx_age ninit_afqsc, vce(robust)
test indinst
local jointF`INST' = r(p)
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { //female
drop r`cov'
}
drop indinst


eststo clear
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { //female
if "`cov'"!= "init_afqsc"  reghdfe `cov'  ${`INST'} , absorb(${`SPEC'} ) $SEa
if "`cov'"== "init_afqsc"  reghdfe `cov'  ${`INST'} , absorb(afqs_mi ${`SPEC'}  ) $SEa
loc dec 2
local `cov'_`INST'_b = _b[${`INST'}] * ${SCALE}
local `cov'_`INST'_se = _se[${`INST'}] * ${SCALE}
if inlist("`cov'","black","hisp","other_race","married","anydep","hsgplus") local `cov'_`INST'_se = _se[${`INST'}] * ${SCALE} * 100
local `cov'_`INST'_se: display %5.`dec'f ``cov'_`INST'_se'
local `cov'_`INST'_se = substr("``cov'_`INST'_se'",-4,.)
if inlist("`cov'","black","hisp","other_race","married","anydep","hsgplus") local `cov'_`INST'_b = _b[${`INST'}] * ${SCALE} * 100
		local `cov'`INST'_p = abs(``cov'_`INST'_b'/``cov'_`INST'_se')
		if ``cov'`INST'_p' < 1.645 local `cov'`INST'_st = ""
		if ``cov'`INST'_p'>= 1.645 local `cov'`INST'_st = "*"
		if ``cov'`INST'_p'>= 1.960 local `cov'`INST'_st = "**"
		if ``cov'`INST'_p'>= 2.576 local `cov'`INST'_st = "***"

}
local n = e(N)
}
cap file close b
	
file open b using "${outputfolder}/Table2.tex", write replace 
	
foreach cov of varlist black hisp other_race married init_nrdep hsgplus init_approx_age init_afqsc { 
if "`cov'" == "black" gl axislab Black
if "`cov'" == "hisp" gl axislab Hispanic
if "`cov'" == "other_race" gl axislab Other Race
if "`cov'" == "married" gl axislab Married
if "`cov'" == "init_nrdep" gl axislab Num. Dependents 
if "`cov'" == "hsgplus" gl axislab HS Graduate +
if "`cov'" == "init_approx_age" gl axislab Age
if "`cov'" == "init_afqsc" gl axislab AFQT

file write b "${axislab}" _char(38) %5.`dec'f (``cov'_NEWINST_b') ("``cov'NEWINST_st'") _char(38) %5.`dec'f (``cov'_NEWENDOG_b') ("``cov'NEWENDOG_st'")  _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``cov'_NEWINST_se'" _char(41) _char(38)  _char(40)"``cov'_NEWENDOG_se'" _char(41) _char(92) _char(92) _n
}

	file write b "\hline" _n
	file write b "Observations &" %9.0gc (`n') _char(38) %9.0gc (`n') _char(92) _char(92) _n
	file write b "P-value on Joint Test &" %5.2f (`jointFNEWINST') _char(38) %5.2f (`jointFNEWENDOG') _char(92) _char(92) _n
	file close b

}




*************
*  Table 3   
*************
	forv i = 1/36 {
			gen amtssdissi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
	}
	
	global runlist anyvadc_in_ anyssdissi_in_  anydisability_in_  amtvadc_in_ amtssdissi_in_ amtdisability_in_  combat_death_by_ wia_by_ any_profile_by_ any_sigprofile_by_ 
	local loop = 0 
	
	eststo clear
	foreach fe in FE1 { 
	foreach dv in $runlist {
		local loop = `loop' + 1
	foreach x in 8 16 24 32 { 
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	
	local dec 2
	if inlist("`dv'","amtdisability_in_","amtvadc_in_", "amtssi_in_", "amtssdi_in_", "amtssdissi_in_", "amtssdissi_v2_in_") local dec 2	
	
		local `dv'`x'_b = _b[${NEWENDOG}] * 10
		local `dv'`x'_se = _se[${NEWENDOG}] * 10
	 	local `dv'`x'_N = e(N)

	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}

if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "anyssdissi_in_" global axislab "Any SSDI or SSI"
if "`dv'" == "anydisability_in_" global axislab "Any Disability"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "amtssdissi_in_" global axislab "Annual Amt SSDI or SSI"
if "`dv'" == "amtdisability_in_" global axislab "Annual Amt Disability"
if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_profile_by_" global axislab "Army Profile"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"

	file open b using "${outputfolder}/Table3_row`loop'.tex", write replace
	file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n
	file close b

}		
}	

	
*************
* Table 4
*************

	global runlist bar_or_misconduct_by_ inc_combined_by_ nsc_enr_postarr_by_ nsc_asscplus_postarr_by_
		
	eststo clear
	local fe FE1 
	local loop = 0
	foreach dv in $runlist {
		local loop = `loop' + 1
		if `loop' == 3 local loop = 4 // skip 3 as its for credit later
	foreach x in 8 16 24 32 { 
	eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local `dv'`x'_b = _b[${NEWENDOG}] * 10
	local `dv'`x'_se = _se[${NEWENDOG}] * 10
	local `dv'`x'_N = e(N)
	
	local dec 2
	local `dv'`x'_se: display %5.`dec'f ``dv'`x'_se'
	if ``dv'`x'_se' < 10 & `dec' == 2 local `dv'`x'_se = substr("``dv'`x'_se'",-4,.)
	
	if `x' == 32 { 
		reg `dv'`x' if e(sample)
		local `dv'_m = _b[_cons]
	}
	
	local `dv'`x'_p = abs(``dv'`x'_b'/``dv'`x'_se')
	if ``dv'`x'_p' < 1.645 local `dv'`x'_star = ""
	if ``dv'`x'_p'>= 1.645 local `dv'`x'_star = "*"
	if ``dv'`x'_p'>= 1.96 local `dv'`x'_star = "**"
	if ``dv'`x'_p'>= 2.576 local `dv'`x'_star = "***"
	
	}
	
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct/Barred"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll Post-arrival"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ Post-arrival"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
	
	file open b using "${outputfolder}/Table4_row`loop'.tex", write replace

		file write b "${axislab}" _char(38) %5.`dec'f (``dv'8_b') ("``dv'8_star'") _char(38) %5.`dec'f (``dv'16_b') ("``dv'16_star'") _char(38) %5.`dec'f (``dv'24_b') ("``dv'24_star'") _char(38) %5.`dec'f (``dv'32_b') ("``dv'32_star'") _char(38) %5.`dec'f (``dv'_m') _char(92) _char(92) _n
file write b "" _char(38) _char(40) "``dv'8_se'" _char(41) _char(38) _char(40) "``dv'16_se'" _char(41) _char(38) _char(40) "``dv'24_se'" _char(41) _char(38) _char(40) "``dv'32_se'" _char(41) _char(38) _char(92) _char(92) "\hline" _n
file write b "Observations" _char(38) "``dv'8_N'" _char(38) "``dv'16_N'" _char(38) "``dv'24_N'" _char(38) "``dv'32_N'" _char(38) "``dv'32_N'" _n

	file close b

}
		
**   --  Credit Scores   --
local runlist vantage_v3_score689 vantage_v4_score731 
qui {
foreach dv in `runlist' {
	local prefix
	ivreghdfe `prefix'`dv' (${NEWENDOG} = ${NEWINST}) , absorb($FE1) robust $ivregsettings
	local `dv'_b = _b[${NEWENDOG}] * 10
	local `dv'_se = _se[${NEWENDOG}] * 10
	summ `prefix'`dv' if e(sample) ==1 
	local `dv'_m = r(mean)
	local `dv'_N = e(N)

	local `dv'_p = abs(``dv'_b'/``dv'_se')
	if ``dv'_p' < 1.645 local `dv'_star = ""
	if ``dv'_p'>= 1.645 local `dv'_star = "*"
	if ``dv'_p'>= 1.96 local `dv'_star = "**"
	if ``dv'_p'>= 2.576 local `dv'_star = "***"
}
}
cap file close b

file open b using "${outputfolder}/Table4_row3.tex", write replace
	file write b "Vantage Score" _char(38) %5.2f (`vantage_v3_score689_b') ("`vantage_v3_score689_star'")
	file write b _char(38) %5.2f (`vantage_v3_score689_m') _char(38) %5.2f (`vantage_v4_score731_b') ("`vantage_v3_score731_star'") _char(38) %5.2f (`vantage_v4_score731_m') _char(92) _char(92) _n
	file write b "" _char(38) _char(40) %5.2f (`vantage_v3_score689_se') _char(41) _char(38) _char(38) _char(40) %5.2f (`vantage_v4_score731_se') _char(41) _char(92) _char(92) _n
	file write b "Observations" _char(38) "`vantage_v3_score689_N'" _char(38) "`vantage_v4_score731_N'" _n
	file close b


*************
* Table 5
*************
global RUNLIST combat_death_by_ wia_by_ any_sigprofile_by_ amtvadc_in_ anyvadc_in_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ bar_or_misconduct_by_ inc_combined_by_ vantage_v4_score731 nsc_enr_postarr_by_ nsc_asscplus_postarr_by_
local loop = 0 
eststo clear
foreach dv in $RUNLIST {
	local loop = `loop' + 1 
	foreach inst2 in casnotsd  {
		if "`inst2'" == "wiarate" local inst2lab = "Peer WIA"
		if "`inst2'" == "casrate" local inst2lab = "Peer Casualties"
foreach x in 32 { 
if substr("`dv'",-3,3) == "by_" |  substr("`dv'",-3,3) == "in_"  {
	ivreghdfe `dv'`x' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	sum `dv'`x' if e(sample)
	local `dv'_m = r(mean) 
}
if substr("`dv'",-3,3) != "by_" &  substr("`dv'",-3,3) != "in_" {
	ivreghdfe `dv' (${NEWENDOG} ${NEWENDOG}_`inst2' = ${NEWINST} ${NEWINST}_`inst2') if ${rest} >= `x' , absorb($FE1) robust $ivregsettings
	sum `dv' if e(sample)
	local `dv'_m = r(mean)  
}

cap drop temp
gen temp = 1 if e(sample)
summ casnotsd if temp == 1 
	local `dv'`x'_b1 = _b[${NEWENDOG}] * 10 
	local `dv'`x'_se1 = _se[${NEWENDOG}] * 10 
	local `dv'`x'_b2 = _b[${NEWENDOG}_`inst2'] * 10 * r(sd)
	local `dv'`x'_se2 = _se[${NEWENDOG}_`inst2'] * 10 * r(sd)

	local dec 2
	if inlist("`dv'","ndi_mv_acc2_by_") local dec 3
	if inlist("`dv'", "amtvadc_in_") local dec 0 
	
	forv i = 1/2 {
	local `dv'`x'_se`i': display %5.`dec'f ``dv'`x'_se`i''
	if ``dv'`x'_se`i'' < 10 & `dec' == 2 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",-4,.)
	if `dec' == 0 local `dv'`x'_se`i' = substr("``dv'`x'_se`i''",strrpos("``dv'`x'_se`i''"," ")+1,.)
	}
	
	forvalues i = 1/2 {
		local `dv'`x'_p`i' = abs(``dv'`x'_b`i''/``dv'`x'_se`i'')
		if ``dv'`x'_p`i'' < 1.645 local `dv'`x'_st`i' = ""
		if ``dv'`x'_p`i''>= 1.645 local `dv'`x'_st`i' = "*"
		if ``dv'`x'_p`i''>= 1.96 local `dv'`x'_st`i' = "**"
		if ``dv'`x'_p`i''>= 2.576 local `dv'`x'_st`i' = "***"
		}
		
if "`dv'" == "combat_death_by_" global axislab "Combat Death"
if "`dv'" == "wia_by_" global axislab "Ever Combat Injury"
if "`dv'" == "any_sigprofile_by_" global axislab "Significant Army Profile"
if "`dv'" == "amtvadc_in_" global axislab "Annual Amt VADC"
if "`dv'" == "anyvadc_in_" global axislab "Any VADC Receipt"
if "`dv'" == "noncom_death_by_" global axislab "Noncombat Death"
if "`dv'" == "ndi_despair2_by_" global axislab "Death of Despair"
if "`dv'" == "ndi_suicide_plus2_by_" global axislab "Suicide"
if "`dv'" == "ndi_drug2_by_" global axislab "Drug- or Alcohol-Rel. Death"
if "`dv'" == "ndi_mv_acc2_by_" global axislab "Motor Vehicle Death"
if "`dv'" == "bar_or_misconduct_by_" global axislab "Separated for Misconduct"
if "`dv'" == "inc_combined_by_" global axislab "Ever Incarcerated"
if "`dv'" == "vantage_v4_score731" global axislab "Credit Score in 2020 (Vantage)"
if "`dv'" == "nsc_enr_postarr_by_" global axislab "Enroll in College"
if "`dv'" == "nsc_asscplus_postarr_by_" global axislab "Assc Deg+ "

	file open b using "${outputfolder}/Table5_row`loop'.tex", write replace
	
	file write b "${axislab}" _char(38) %6.`dec'f (``dv'`x'_b1') ("``dv'`x'_st1'") _char(38) %6.`dec'f (``dv'`x'_b2') ("``dv'`x'_st2'") _char(38) %6.`dec'f (``dv'_m') _char(92) _char(92) _n
	file write b "" _char(38) _char(40) "``dv'`x'_se1'" _char(41) _char(38) _char(40) "``dv'`x'_se2'" _char(41) _char(38) _char(92) _char(92) _n

	file close b

}
}
}


